﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.Web.UI;


/// <summary>
/// Summary description for Util
/// </summary>
public static class Util
{
    // Entity Framework get current item craziness: http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/a6c0a1f0-349f-4163-8fcb-a0c5a1e7c5a6/
    public static TEntity GetUnderlyingEntity<TEntity>(this System.Web.UI.Page page, object dataItem) where TEntity : class
    {
        var entity = dataItem as TEntity;
        if (entity != null) return entity;

        var td = dataItem as ICustomTypeDescriptor;
        if (td != null)
        {
            return (TEntity)td.GetPropertyOwner(null);
        }

        return null;
    }

    public static void MessageBox(this Page page, string message, string redirectUrl)
    {
        page.Session["MessageForm.Message"] = message;
        page.Session["MessageForm.Redirect"] = redirectUrl;
        page.Response.Redirect("~/MessageForm.aspx");
    }
}
